]<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">{{store.state.settings.title}}</div>
      <el-form :model="logindto" :rules="rules" ref="login" label-width="0px" class="ms-content">
        <el-form-item prop="loginName">
          <el-input v-model="logindto.loginName" placeholder="username">
            <template #prepend>
              <el-button :icon="User"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              type="password"
              placeholder="password"
              v-model="logindto.password"
              @keyup.enter.native="submitForm()"
          >
            <template #prepend>
              <el-button :icon="Lock"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input placeholder="请输入验证码" v-model="logindto.imageCode">
            <template #append>
              <img v-on:click="loadImageCode()" style="height: 26px" :src="codeurl" id="image-code" alt="验证码"/>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox-group v-model="remember">
            <el-checkbox label="记住密码" name="type"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm(login)">登录</el-button>
        </div>
        <p class="login-tips">没有账号</p>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref ,inject,reactive,onMounted} from 'vue';
import {userlogin} from '@/api/user';
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Lock, User } from '@element-plus/icons-vue';
import {useStore} from "vuex";
const router:any = useRouter()
const logindto:any= reactive({});
const rules:any ={
  loginName: [{required: true, message: '请输入用户名', trigger: 'blur'}],
  password: [{required: true, message: '请输入密码', trigger: 'blur'}],
};
const remember:any=ref();
const LocalStorage:any = inject("$LocalStorage");
const Tool:any = inject("$Tool");
const MD5:any = inject("$MD5");
const imageCodeToken:any=ref('');
const store: any = useStore();
const codeurl:any=ref('');
const login:any= ref();
const submitForm:any=(login:any)=> {
  // 如果密码是从缓存带出来的，则不需要重新加密
  let md5 = MD5.hex_md5(logindto.password);
  let rememberUser = LocalStorage.get(LocalStorage.LOCAL_KEY_REMEMBER_USER) || {};
  if (md5 !== rememberUser.md5) {
    logindto.password = MD5.hex_md5(logindto.pass + MD5.KEY);
  }
  logindto.imageCodeToken =imageCodeToken;
  login.validate((valid:any) => {
    if (valid) {
      userlogin('/process/admin/user/login', logindto).then(async (res: any) => {
        ElMessage.success('登录成功');
        Tool.setLoginUser(res);
        // 判断“记住我”
        if (remember) {
          // 如果勾选记住我，则将用户名密码保存到本地缓存
          // 原：这里需要保存密码明文，否则登录时又会再加一层密
          // 新：这里保存密码密文，并保存密文md5，用于检测密码是否被重新输入过
          let md5 = MD5.hex_md5(logindto.password);
          LocalStorage.set(LocalStorage.LOCAL_KEY_REMEMBER_USER, {
            loginName: res.loginName,
            password: logindto.password,
            md5: md5
          });
        } else {
          // 没有勾选“记住我”时，要把本地缓存清空，否则按照mounted的逻辑，下次打开时会自动显示用户名密码
          LocalStorage.set(LocalStorage.LOCAL_KEY_REMEMBER_USER, null);

        }
        window.location.href = '/admin/dashboard';
      })
    }
  });
}

const loadImageCode:any=()=>{
  imageCodeToken.value = Tool.uuid(8);
  codeurl.value = '/api/process/admin/kaptcha/image-code/' + imageCodeToken.value;
}
onMounted(()=>{
  loadImageCode();
});

</script>

<style>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  /*background-image: url(@/assets/img/login-bg.jpg);*/
  background-size: 100%;
}

.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}

.append {
  background-color: #F5F7FA;
  color: #909399;
  vertical-align: middle;
  display: table-cell;
  position: relative;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-radius: 4px;
  width: 1px;
  white-space: nowrap;
  padding: 0px !important;
  background-color: #f5f7fa00 !important;
  border: 0px !important;
}

.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}

.ms-content {
  padding: 30px 30px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}

.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
</style>
